import Vue from 'vue'
import VueRouter from 'vue-router'
// 模块之间需要引入才能使用，引入仓库
import store from '@/store'
// 引入组件
import Layout from '@/views/layout'
import Login from '@/views/login'
import Dashboard from '@/views/dashboard'
import OperationOrder from '@/views/operationorder'
import MaintenanceOrder from '@/views/maintenanceorder'
import AreaManagement from '@/views/areamanagement'
import PointManagement from '@/views/pointmanagement'
import PartnerManagement from '@/views/partnermanagement'
import DeviceManagement from '@/views/devicemanagement'
import DeviceStatus from '@/views/devicestatus'
import DeviceType from '@/views/devicetype'
import PersonList from '@/views/personlist'
import PersonTotal from '@/views/persontotal'
import WorkLoad from '@/views/workload'
import GoodsType from '@/views/goodstype'
import GoodsManage from '@/views/goodsmanage'
import PolicyManage from '@/views/policymanage'
import OrderManage from '@/views/ordermanage'
import MoneyManage from '@/views/moneymanage'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    {
      path: '/',
      component: Layout,
      redirect: '/dashboard',
      children: [
        { path: '/dashboard', component: Dashboard },
        { path: '/operationorder', component: OperationOrder },
        { path: '/maintenanceorder', component: MaintenanceOrder },
        { path: '/areamanagement', component: AreaManagement },
        { path: '/pointmanagement', component: PointManagement },
        { path: '/partnermanagement', component: PartnerManagement },
        { path: '/devicemanagement', component: DeviceManagement },
        { path: '/devicestatus', component: DeviceStatus },
        { path: '/devicetype', component: DeviceType },
        { path: '/personlist', component: PersonList },
        { path: '/persontotal', component: PersonTotal },
        { path: '/workload', component: WorkLoad },
        { path: '/goodstype', component: GoodsType },
        { path: '/goodsmanage', component: GoodsManage },
        { path: '/policymanage', component: PolicyManage },
        { path: '/ordermanage', component: OrderManage },
        { path: '/moneymanage', component: MoneyManage }
      ]
    }
  ]
})
const whiteList = ['/login']
// 路由前置守卫，每次进入一个路由地址，都会触发这个函数
router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  if (token) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
